#include "ReadHighscores.h"

ReadHighscores::ReadHighscores(void)
{
}

ReadHighscores::~ReadHighscores(void)
{
}

Highscore** ReadHighscores::GetHighscores(){

	//highscores.txt  looks like this:
	// Time:Deaths:Points
	ifstream inFile("res/highscores.txt");
	Highscore* h;
	Highscore* scores[10];

	char c;
	char l[40];
	int i = 0;

	int currentDeaths;
	int currentTime;
	int currentPoints;
	int foundColon;
	inFile.getline(l,40);

	while(inFile.getline(l,40) && i < 10){
		int atoiValues = 0;
		int atoiValue = 0;
		currentDeaths = 0;
		currentTime = 0;
		currentPoints = 0;
		int index = 0;
		c = l[0];
		foundColon = 0;
		while(c != NULL && index < 40){
			
			if(c == ':'){
				foundColon++;
			}
			else{
				if(foundColon == 0){
					if(currentTime == 0){
						atoiValue = atoi(&c);
						currentTime = atoiValue;
						
					}
					else{
						atoiValue = atoi(&c);
						currentTime = (currentTime * 10) +  atoiValue;
					}
				}
				else if(foundColon == 1){
					if(currentDeaths == 0){
						atoiValue = atoi(&c);
						currentDeaths =  atoiValue;
					}
					else{
						atoiValue = atoi(&c);
						currentDeaths = (currentDeaths * 10) +  atoiValue;
					}
				}
				else{
					if(currentPoints == 0){
						atoiValue = atoi(&c);
						currentPoints =  atoiValue;
					}
					else{
						atoiValue = atoi(&c);
						currentPoints = (currentPoints * 10) +  atoiValue;
					}
				}

				atoiValues = (atoiValues*10) + atoiValue;
			}

			index++;
			c = l[index];
		}
		h= new Highscore(currentTime, currentDeaths, currentPoints);
		scores[i] =h;
		i++;

			if(currentPoints == 8088){
				currentPoints = 8088;
			}
		
	}

	return scores;
}